function out = frameRec2(coeff, dlevel, rlevel, HR0)
%This program performs framelet reconstruction.
%parameters:
%coeff : the input coefficients. Type : cell.
%dlevel : the desctruction level of the original signal . Type : int
%rlevel : to which level the program reconstructs.  Type :int.
%HR0 : the framelet reconstruction filter. Type cell.
%output:
%out : the reconstruction result . Type : cell.
nfilters = numel(HR0);
[M N] = size(coeff{1});
mSignal= M/nfilters;
nSignal = N/nfilters;
out = [];
HR{1} = HR0;
for i = 2 : dlevel
    HR{i} = doubleFilter(HR{i-1});
end

currentLevel = dlevel;
while currentLevel > rlevel
    lenFilter = length(HR{currentLevel}{1});
    cutcolumn = zeros(mSignal, N);
    tmpSig = zeros(mSignal,N);
    column = zeros(mSignal + lenFilter*2 -2, N);
    for k = 1 : nfilters
        tmpSig(:,:) = coeff{currentLevel}( (k-1)*mSignal+1 : k*mSignal,:);
        eexttmpSig = [tmpSig(mSignal - (lenFilter-3)/2 : mSignal,:); ...
            tmpSig(:,:);
            tmpSig(1:(lenFilter-1)/2,:)];
        column = conv2(eexttmpSig', HR{currentLevel}{k})';
        cutcolumn= cutcolumn + column(lenFilter : lenFilter -1+mSignal, :);
    end
    
    cutrow = zeros(mSignal, nSignal);
    for k = 1 : nfilters
        tmpSig = cutcolumn(:,(k-1)*nSignal+1 : k*nSignal);
        exttmpSig = [tmpSig(:,nSignal - (lenFilter-3)/2 : nSignal) tmpSig(:,:) tmpSig(:,1:(lenFilter-1)/2)];
        row = conv2(exttmpSig,HR{currentLevel}{k});
        cutrow = cutrow + row(:, lenFilter : lenFilter -1 + nSignal);
    end
    
    if currentLevel == 1
        coeff(currentLevel) = [];
        coeff{currentLevel} = cutrow;
    else
        coeff(currentLevel) = [];
        coeff{currentLevel-1}(1:mSignal, 1:nSignal) = cutrow;
    end
    currentLevel = currentLevel -1;
end
out = coeff;
if rlevel == 0
    out = cell2mat(out);
end
end




